package com.authManager.server.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

/**
 * 说明：
 * 
 * @author: 小旋风
 */

public class BaseController {
	/**
	 * 默认返回的数据格式 application/json;charset=UTF-8
	 */
	public static final String defProduces = "application/json;charset=UTF-8";
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	
	public String getCurrUserLoginId() {
		return null;
	}
	public ResponseEntity<FileSystemResource> exportFile(File file, String downName) {
		if (file == null) {
			return null;
		}
		HttpHeaders headers = new HttpHeaders();
		headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
		headers.add("Content-Disposition", "attachment; filename=" + downName);
		headers.add("Pragma", "no-cache");
		headers.add("Expires", "0");
		return ResponseEntity.ok().headers(headers).contentLength(file.length())
				.contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
	}

	public ResponseEntity<ByteArrayResource> exportByte(byte[] bytes, String downName) {
		if (bytes == null) {
			return null;
		}
		HttpHeaders headers = new HttpHeaders();
		headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
		try {
			headers.add("Content-Disposition",
					"attachment; filename=" + new String(downName.getBytes("UTF-8"), "iso-8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		headers.add("Pragma", "no-cache");
		headers.add("Expires", "0");
		return ResponseEntity.ok().headers(headers).contentLength(bytes.length)
				.contentType(MediaType.parseMediaType("application/octet-stream")).body(new ByteArrayResource(bytes));
	}
}
